<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
/**
 * This is the model class for table "push".
 *
 * @property integer $pushid
 * @property integer $contentid
 * @property integer $user_id
 * @property string $title
 * @property string $content
 * @property integer $state
 * @property integer $type
 * @property integer $created_at
 * @property integer $updated_at
 */
class Push extends ActiveRecord
{
    const STATE_READ = 1;
    const STATE_NOT_READ = 0;

    const TYPE_ADMIN = 0;
    const TYPE_AUCTION = 1;

    const SEND_IS = 1;
    const SEND_NOT = 0;

    public static function dropdown($column, $value = null)
    {
        $dropDownList = [
            'state'=>[
                self::STATE_READ=>'已读',
                self::STATE_NOT_READ=>'未读',
            ],
            'is_send'=>[
                self::SEND_IS => '已发送',
                self::SEND_NOT=>'未发送',
            ]
        ];
        if ($value !== null)
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column][$value] : false;
        else
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column] : false;
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'push';
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes'=>[
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ]
        ];
    }
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[ 'title', 'content'], 'required'],
            [['content'], 'string'],
            [['title'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'pushid' => '推送id',
            'contentid' => '内容id',
            'user_id' => '用户id',
            'title' => '标题',
            'type'=>'消息',
            'content' => '内容',
            'state' => '状态',
            'is_send'=>'是否发送',
            'created_at' => '创建时间',
            'updated_at' => '修改时间',
        ];
    }

    public static function create($title,$content,$user_id,$type = self::TYPE_ADMIN)
    {
        $model = new self();
        $model->title = $title;
        $model->content = $content;
        $model->contentid = self::getContentid();
        $model->user_id = $user_id;
        $model->type = $type;
        $model->save(false);
    }

    public static function getContentid()
    {
        $maxcontentid = Push::find()->max('contentid');
        $contentid = $maxcontentid?$maxcontentid+1:1;
        return $contentid;
    }
}
